import 'package:flutter/material.dart';
import './homes/home.dart';
import './homes/news.dart';
import './homes/user.dart';

class Homes extends StatefulWidget {
  const Homes({super.key});

  @override
  State<Homes> createState() => _HomesState();
}

class _HomesState extends State<Homes> {
  int _currentIndex = 0;
  //页面控制器
  late PageController _pageController;

  @override
  void initState() {
    super.initState();
    //初始化控制器进来显示那个页面
    _pageController = PageController(initialPage: _currentIndex);
  }

  final List<Widget> _pages = const [
    HomePage(),
    NewsPage(),
    UserPage(),
  ];
  //final _childKey = GlobalKey<State<CategoryPage>>();
  //页面切换导航切换
  void _onPageChanged(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      /*  appBar: AppBar(
        title: const Text("CAN_WIFI"),
        actions: <Widget>[
          IconButton(onPressed: (() {}), icon: const Icon(Icons.more_horiz)),
        ],
      ), */
      drawer: Drawer(
        child: Column(
          children: [
            Row(
              // ignore: prefer_const_literals_to_create_immutables
              children: [
                const Expanded(
                    flex: 1,
                    child: UserAccountsDrawerHeader(
                      accountName: Text("动画1班任同学"),
                      accountEmail: Text("ren.yuesheng@163.com"),
                      /*  otherAccountsPictures: [
                        Image.network(r
                            "https://www.itying.com/images/flutter/1.png"),
                        Image.network(
                            "https://www.itying.com/images/flutter/2.png"),
                        Image.network(
                            "https://www.itying.com/images/flutter/3.png"),
                      ], */
                      currentAccountPicture: CircleAvatar(
                          backgroundImage:
                              AssetImage('assets/images/head1.jpeg')),
                      decoration: BoxDecoration(
                          image: DecorationImage(
                              fit: BoxFit.cover,
                              image: AssetImage('assets/images/school.jpg'))),
                    ))
              ],
            ),
            const ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.people),
              ),
              title: Text("个人中心"),
            ),
            const Divider(),
            const ListTile(
              leading: CircleAvatar(
                child: Icon(Icons.settings),
              ),
              title: Text("系统设置"),
            ),
            const Divider(),
          ],
        ),
      ),
      endDrawer: const Drawer(
        child: Text("右侧侧边栏"),
      ),
      //body: _pages[_currentIndex],
      //缓存页面用indexstack,防止切换路由数据丢失，有缺点
      /*  body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
 */
      //PageView中配置controller控制器，用于缓存页面
      body: PageView(
        controller: _pageController,
        onPageChanged: _onPageChanged, ////页面切换导航切换
        children: _pages,
      ),

      bottomNavigationBar: BottomNavigationBar(
          fixedColor: Colors.blue, //选中的颜色
          // iconSize:35,           //底部菜单大小
          currentIndex: _currentIndex, //第几个菜单选中
          type: BottomNavigationBarType.fixed, //如果底部有4个或者4个以上的菜单的时候就需要配置这个参数
          onTap: (index) async {
            //点击菜单触发的方法
            //注意
            setState(() {
              _currentIndex = index;
              //点击tab时更新控制器中的index
              _pageController.jumpToPage(_currentIndex);
              print(_currentIndex);
            });
          },
          items: const [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
            //BottomNavigationBarItem(icon: Icon(Icons.settings), label: "参数"),
            BottomNavigationBarItem(icon: Icon(Icons.message), label: "创业栏"),
            //BottomNavigationBarItem(icon: Icon(Icons.ad_units), label: "设备"),
            BottomNavigationBarItem(icon: Icon(Icons.person), label: "我的")
          ]),
      /* floatingActionButton: Container(
        height: 60, //调整FloatingActionButton的大小
        width: 60,
        padding: const EdgeInsets.all(5),
        margin: const EdgeInsets.only(top: 5), //调整FloatingActionButton的位置
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(30),
        ),
        child: FloatingActionButton(
            backgroundColor: _currentIndex == 2 ? Colors.blue : Colors.blue,
            child: const Icon(Icons.add),
            onPressed: () {
              setState(() {
                _currentIndex = 2;
                _pageController.jumpToPage(_currentIndex);
              });
            }),
      ),
      floatingActionButtonLocation:
          FloatingActionButtonLocation.centerDocked, //配置浮动按钮的位置 */
    );
  }
}
